<template>
	<view class="main-container">

		<image class="logo" src="/static/logo.png" />
		<view class="header-wrapper">
			<view class="title">东华冶金矿业集团有限公司
			</view>
		</view>


		<view class="content">
			<view class="title">矿种选择</view>
			<view class="tag-wrappper">
				<view class="tag" v-for="(item,index) in mineralSpeciesList" :key="index">
					<u-tag :text="item.name" :plain="!item.select" @click="tagClick(item)"></u-tag>
				</view>
			</view>
			<u--form labelPosition="left" :model="model" :rules="rules" ref="form">
				<u-form-item label="储量" prop="reserves" borderBottom labelWidth="160rpx" :required="true">
					<u--input v-model="model.reserves" border="none" placeholder="请输入储量" type="digit" :clearable="true"
						suffixIcon="万吨" :suffixIconStyle="suffixIconStyle">
					</u--input>
				</u-form-item>
				<u-form-item label="矿体倾角" prop="dipAngle" borderBottom labelWidth="160rpx" :required="true">
					<u--input v-model="model.dipAngle" border="none" placeholder="请输入矿体倾角" type="digit"
						:clearable="true" suffixIcon="度" :suffixIconStyle="suffixIconStyle">
					</u--input>
				</u-form-item>
				<u-form-item label="矿体厚度" prop="thickness" borderBottom labelWidth="160rpx" :required="true">
					<u--input v-model="model.thickness" border="none" placeholder="请输入矿体厚度" type="digit"
						:clearable="true" suffixIcon="m" :suffixIconStyle="suffixIconStyle">
					</u--input>
				</u-form-item>
				<u-form-item label="矿体长度" prop="lengths" borderBottom labelWidth="160rpx" :required="true">
					<u--input v-model="model.lengths" border="none" placeholder="请输入矿体长度" type="digit" :clearable="true"
						suffixIcon="m" :suffixIconStyle="suffixIconStyle">
					</u--input>
				</u-form-item>
				<u-form-item label="矿体埋深" prop="buriedDepth" borderBottom labelWidth="160rpx" :required="true">
					<u--input v-model="model.buriedDepth" border="none" placeholder="请输入矿体埋深" type="digit"
						:clearable="true" suffixIcon="m" :suffixIconStyle="suffixIconStyle">
					</u--input>
				</u-form-item>
				<view v-for="(item,index) in  model.mineralInputList" :key="index">
					<u-form-item :label="item.lable" :prop="model.mineralInputList[index].taste" borderBottom
						labelWidth="160rpx" :required="true">
						<u--input v-model="model.mineralInputList[index].taste" border="none" placeholder="请输入品位"
							type="digit" :clearable="true" suffixIcon="%" :suffixIconStyle="suffixIconStyle">
						</u--input>
					</u-form-item>
				</view>
			</u--form>

			<!-- 各矿种品位输入-->
			<u-modal :show="dialogShow" :title="title" :showCancelButton="true" @cancel="dialogShow=false"
				@confirm="updateMineral">
				<view class="modal-content">{{content}}</view>
			</u-modal>
		</view>
		<view class="motto-box">
			<image class="motto" src="/static/motto.png" />
		</view>

		<view class="u-button-wrap">
			<view class="button-wrapper">
				<u-button type="primary" text="提交" @click="submit"></u-button>
			</view>
		</view>
	</view>
</template>

<script>
	import Decimal from 'decimal.js'
	export default {
		data() {
			return {
				title: '添加矿种',
				content: '您确定添加矿种么？',
				dialogShow: false,
				currentIndex: 0,
				//矿种列表
				mineralSpeciesList: [],
				model: {
					//储量
					reserves: '',
					//矿体倾角
					dipAngle: '',
					//矿体厚度
					thickness: '',
					//矿体长度
					lengths: '',
					//矿体埋深
					buriedDepth: '',
					//矿种输入列表
					mineralInputList: [],
				},
				rules: {
					'reserves': {
						type: 'string',
						required: true,
						message: '请填写储量',
						trigger: ['blur', 'change']
					},
					'dipAngle': {
						type: 'string',
						required: true,
						message: '请填写矿体倾角',
						trigger: ['blur', 'change']
					},
					'thickness': {
						type: 'string',
						required: true,
						message: '请填写矿体厚度',
						trigger: ['blur', 'change']
					},
					'lengths': {
						type: 'string',
						required: true,
						message: '请填写矿体长度',
						trigger: ['blur', 'change']
					},
					'buriedDepth': {
						type: 'string',
						required: true,
						message: '请填写矿体埋深',
						trigger: ['blur', 'change']
					},
				},
				//后置图标样式
				suffixIconStyle: {
					fontSize: 16
				}

			}
		},
		onLoad() {
			let mineralSpeciesList = [];
			//复制对象
			for (let item of this.$constant.mineralSpeciesList) {
				mineralSpeciesList.push({
					id: item.id,
					name: item.name,
					select: false,
					concentrateGrade: item.concentrateGrade,
					beneficiationRecovery: item.beneficiationRecovery,
					refinedPowderunitPrice: item.refinedPowderunitPrice,
					//系数
					coefficient: item.coefficient,
					//采矿
					mining: item.mining,
					//选矿
					mineralProcessing: item.mineralProcessing
				})
			}
			this.mineralSpeciesList = mineralSpeciesList;
		},
		methods: {
			/**
			 * 提交
			 */
			submit() {
				// 如果有错误，会在catch中返回报错信息数组，校验通过则在then中返回true
				this.$refs.form.validate().then(res => {
					if (this.model.mineralInputList.length == 0) {
						uni.$u.toast('请选择至少一个矿种')
						return
					}
					let isfillIn = true;
					for (let minerral of this.model.mineralInputList) {
						if (!minerral.taste) {
							uni.$u.toast('请填写' + minerral.lable)
							isfillIn = false;
							break;
						}

					}
					if (isfillIn) {
						this.submitForm();
					}
				}).catch(errors => {


				})
			},
			submitForm() {
				//开始计算
				const resultList = [];
				for (let minerral of this.model.mineralInputList) {
					let result = {
						title: '矿种（' + minerral.name + '）',
						//出矿品味 =品味 *0.9
						taste: new Decimal(minerral.taste).times(0.9).toNumber(),
						//年矿石产量 =储量/20
						oreProduction: new Decimal(this.model.reserves).dividedBy(20).toNumber()
					}
					//精矿产量 =年矿石产量*选矿回收率/100*出矿品位/精矿品位
					result.concentrateProduction = new Decimal(result.oreProduction).times(minerral.beneficiationRecovery)
						.dividedBy(100).times(result.taste).dividedBy(minerral.concentrateGrade).toNumber()
					//精矿品位
					result.concentrateGrade = minerral.concentrateGrade;
					//选矿回收率
					result.beneficiationRecovery = minerral.beneficiationRecovery;
					//基建工程量
					result.projectMeasure = 0;
					//基建期=IF(矿体埋深=0,0,矿体埋深/100*0.05*3+3)
					let capitalConstructionPeriod = 0;
					if (this.model.buriedDepth == 0) {
						capitalConstructionPeriod = 0;
					} else {
						capitalConstructionPeriod = new Decimal(this.model.buriedDepth).dividedBy(100).times(0.05).times(3)
							.plus(3).toNumber();
					}
					result.capitalConstructionPeriod = capitalConstructionPeriod;
					//投资估算 =IF(储量=0,0,年矿石产量*250)  不同矿种系数不一样
					let investmentEstimation = 0;
					if (this.model.reserves == 0) {
						investmentEstimation = 0;

					} else {
						investmentEstimation = new Decimal(result.oreProduction).times(minerral.coefficient).toNumber()
					}

					result.investmentEstimation = investmentEstimation;
					//采矿 =IF(品位=0,0,70)
					result.mining = minerral.taste == 0 ? 0 : minerral.mining;
					//选矿 =IF(品位=0,0,50)
					result.mineralProcessing = minerral.taste == 0 ? 0 : minerral.mineralProcessing;
					//生产成本=(采矿+选矿)*年矿石产量
					result.productionCosts = new Decimal(result.mining).plus(result.mineralProcessing).times(result
						.oreProduction).toNumber();
					//精粉单价
					result.refinedPowderunitPrice = minerral.refinedPowderunitPrice;
					//销售收入=精矿产量*精粉单价
					result.salesRevenue = new Decimal(result.concentrateProduction).times(result.refinedPowderunitPrice)
						.toNumber();
					//上交增值税=(销售收入*0.13)-生产成本*0.07
					result.valueAddedTax = new Decimal(result.salesRevenue).times(0.13).minus(new Decimal(result
						.productionCosts).times(0.07)).toNumber();
					//城建税=上交增值税*0.05
					result.urbanConstructionTax = new Decimal(result.valueAddedTax).times(0.05).toNumber();
					//教育费附加=上交增值税*0.05
					result.educationSurcharges = new Decimal(result.valueAddedTax).times(0.05).toNumber();
					//资源税=销售收入*0.06
					result.resourceTax = new Decimal(result.salesRevenue).times(0.06).toNumber();
					//年利润=销售收入-生产成本-上交增值税-城建税-教育费附加-资源税
					result.annualProfit = new Decimal(result.salesRevenue).minus(result.productionCosts).minus(result
						.valueAddedTax).minus(result.urbanConstructionTax).minus(result.educationSurcharges).minus(
						result.resourceTax).toNumber();
					//缴纳所得税=年利润*0.25
					result.payIncomeTax = new Decimal(result.annualProfit).times(0.25).toNumber();
					//净利润=年利润-缴纳所得税
					result.netProfit = new Decimal(result.annualProfit).minus(result.payIncomeTax).toNumber();
					//投资回收期=投资估算/净利润
					result.investmentPaybackPeriod = new Decimal(result.investmentEstimation).dividedBy(result.netProfit)
						.toNumber();

					//全周期收益=净利润*20
					result.fullCycleReturns = new Decimal(result.netProfit).times(20).toNumber();
					//100%矿权转让费=全周期收益*0.15
					result.transferFee = new Decimal(result.fullCycleReturns).times(0.15).toNumber();
					//统一保留小数
					//for (const key in result) {
						//if (key != 'title' && result[key]) {

							//result[key] = new Decimal(result[key]).toDecimalPlaces(3).toString()
						//}
					//}
					resultList.push(result)
				}
				//总计
				const totalResult = {
					title: '计算结果',
					//出矿品味
					taste: '',
					//年矿石产量 
					oreProduction: Math.max(...resultList.map(item => item.oreProduction)),
					//精矿产量
					concentrateProduction: '',
					//精矿品位
					concentrateGrade: '',
					//选矿回收率
					beneficiationRecovery: '',
					//基建期
					capitalConstructionPeriod: Math.max(...resultList.map(item => item.capitalConstructionPeriod)),
					//投资估算
					investmentEstimation: Math.max(...resultList.map(item => item.investmentEstimation)),
					//采矿
					mining: Math.max(...resultList.map(item => item.mining)),
					//选矿
					mineralProcessing: new Decimal(Math.max(...resultList.map(item => item.mineralProcessing))).times(
						1.5).toNumber(),
				}

				//生产成本=(采矿+选矿)*年矿石产量 
				totalResult.productionCosts = new Decimal(totalResult.mining).plus(totalResult.mineralProcessing).times(
					totalResult.oreProduction).toNumber();

				//销售收入
				totalResult.salesRevenue = resultList.reduce((sum, item) => sum + Number(item.salesRevenue), 0);
				//精粉单价
				totalResult.refinedPowderunitPrice = '';
				//上交增值税=(销售收入*0.13)-生产成本*0.07
				totalResult.valueAddedTax = new Decimal(totalResult.salesRevenue).times(0.13).minus(new Decimal(totalResult
					.productionCosts).times(0.07)).toNumber();

				//城建税=P24*0.05
				totalResult.urbanConstructionTax = new Decimal(totalResult.valueAddedTax).times(0.05).toNumber();
				//教育费附加=P24*0.05
				totalResult.educationSurcharges = new Decimal(totalResult.valueAddedTax).times(0.05).toNumber();
				//资源税=P21*0.06
				totalResult.resourceTax = new Decimal(totalResult.salesRevenue).times(0.06).toNumber();

				//年利润=销售收入-生产成本-上交增值税-城建税-教育费附加-资源税
				totalResult.annualProfit = new Decimal(totalResult.salesRevenue).minus(totalResult.productionCosts)
					.minus(totalResult.valueAddedTax).minus(totalResult.urbanConstructionTax).minus(totalResult
						.educationSurcharges).minus(totalResult.resourceTax).toNumber();
				//缴纳所得税=年利润*0.25
				totalResult.payIncomeTax = new Decimal(totalResult.annualProfit).times(0.25).toNumber();
				//净利润=年利润-缴纳所得税
				totalResult.netProfit = new Decimal(totalResult.annualProfit).minus(totalResult.payIncomeTax).toNumber();;
				//投资回收期=投资估算/净利润
				totalResult.investmentPaybackPeriod = new Decimal(totalResult.investmentEstimation).dividedBy(totalResult
					.netProfit).toNumber();

				//全周期收益=净利润*20
				totalResult.fullCycleReturns = new Decimal(totalResult.netProfit).times(20).toNumber();
				//100%矿权转让费=全周期收益*0.15
				totalResult.transferFee = new Decimal(totalResult.fullCycleReturns).times(0.15).toNumber();
				//统一保留小数
				for (const key in totalResult) {
					if (key != 'title' && totalResult[key]) {
						totalResult[key] = new Decimal(totalResult[key]).toDecimalPlaces(3).toString()
					}
				}
				resultList.push(totalResult)
				this.$store.dispatch('setResultListAction', [totalResult])
				this.$util.navigateTo(this.$config.path.calculateResult)
			},
			/**标签点击
			 * @param {Object} item
			 */
			tagClick(item) {
				this.currentIndex = item.id
				if (item.select) {
					this.title = '移除矿种'
					this.content = '您确定移除' + item.name + '矿么？'
					this.dialogShow = true
				} else {
					this.model.mineralInputList.push({
						id: item.id,
						name: item.name,
						concentrateGrade: item.concentrateGrade,
						beneficiationRecovery: item.beneficiationRecovery,
						coefficient: item.coefficient,
						mining: item.mining,
						mineralProcessing: item.mineralProcessing,
						refinedPowderunitPrice: item.refinedPowderunitPrice,
						lable: '品位(' + item.name + ')',
						taste: ''
					})
					item.select = !item.select;
					this.dialogShow = false;
				}


			},
			/**
			 * 更新矿种
			 */
			updateMineral() {
				if (this.title == '添加矿种') {



				} else {
					let mineral = this.mineralSpeciesList[this.currentIndex]
					for (let i = 0; i < this.model.mineralInputList.length; i++) {
						if (this.model.mineralInputList[i].name == mineral.name) {
							this.model.mineralInputList.splice(i, 1)
							break
						}
					}
					mineral.select = !mineral.select;
					this.dialogShow = false;

				}

			},
		}
	}
</script>

<style lang="scss">
	.main-container {
		padding-top: 60rpx;
		padding-bottom: 140rpx;



		.logo {
			width: 85rpx;
			height: 65rpx;
			top: 65rpx;
			left: 30rpx;
			position: absolute;
		}

		.motto-box {
			display: flex;
			justify-content: center;
			margin-top: 20rpx;
			margin-bottom: 20rpx;

			.motto {
				width: 395rpx;
				height: 75rpx;
			}
		}


		.header-wrapper {
			width: 100%;
			display: flex;
			justify-content: space-between;
			align-items: center;




			.title {
				width: 100%;
				font-size: 32rpx;
				text-align: center;

			}
		}

		.content {
			padding-left: 30rpx;
			padding-right: 30rpx;
			overflow-y: scroll;
		}

		.u-button-wrap {
			margin: 0rpx 0 0rpx 0;
			display: flex;
			flex-direction: row;
			justify-content: center;
			background-color: $uni-color-bg;
			width: 100%;
			position: fixed;
			bottom: 0;
			height: 80rpx;
			padding-top: 30rpx;
			padding-bottom: 30rpx;

			.button {
				width: 100%;
				margin-left: 20rpx;
			}

			.button-last {
				margin-right: 20rpx;
			}

			.button-wrapper {
				margin-left: 20rpx;
				margin-right: 20rpx;
				width: 100%;

			}
		}

		.button-box {
			display: flex;
			width: 100%;
			position: absolute;
			bottom: 0;

		}

		.modal-content {
			font-size: 26rpx;
			color: #fe0403
		}

		.title {
			font-size: 28rpx;
			color: #666666;
			margin-top: 16rpx;
			margin-bottom: 16rpx;
		}

		.tag-wrappper {
			display: flex;
			flex-direction: row;
			flex-wrap: wrap;

			.tag {
				margin-right: 30rpx;
				margin-bottom: 20rpx;
			}

		}


	}
</style>
